using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using NDomain.Reflection;

namespace NDomain.Reflection
{
    public class RepositoryProcessor : BaseClassProcessor
    {
        public override void Process(IClassInfoProvider clazz, IDomainInfo domainInfo)
        {
            IDictionary<string, IAttributeInfoProvider> attributes = AttributesAsDictionary(clazz.GetAttributes());

            if (attributes.ContainsKey(typeof (RepositoryAttribute).FullName))
                DelayProcessing(delegate
                                    {
                                        Regex reg = new Regex(String.Format(@"ICollection\<{0}.([^\>]+)\>", clazz.GetNamespace()));
                                        foreach (string implementedInterface in clazz.GetImplementedInterfaces())
                                        {
                                            Match match = reg.Match(implementedInterface);
                                            if (match.Success)
                                                domainInfo.NewRepository(clazz.GetNamespace(), clazz.GetName(),
                                                                         match.Groups[1].Value);
                                        }
                                    });
        }
    }
}